package com.sword.controller.system;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @RequestMapping("/login")
    public Map login(String username, String password){
        //layui登录json
        Map map = new HashMap();
        Map map1 = new HashMap();
        map.put("code", 0);
        map1.put("access_token", "c262e61cd13ad99fc650e6908c7e5e65b63d2f32185ecfed6b801ee3fbdd5c0a");
        map.put("data", map1);

        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);

        try {
            subject.login(token);
            map.put("msg", "登录成功");
            map.put("success",true);
            return map;
        }catch (UnknownAccountException e){
            map.put("msg", "用户名错误");
            map.put("success",false);
            return map;
        }catch (IncorrectCredentialsException e){
            map.put("msg", "密码错误");
            map.put("success",false);
            return map;
        }

    }
}
